<!--
 * @Author: your name
 * @Date: 2021-02-26 17:25:21
 * @LastEditTime: 2021-03-01 15:40:13
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \vue_test\src\components\Home.vue
-->
<template>
  <el-container class="home-container">
    <!--头部区域-->
    <el-header>
      <div>
        <img src="@/assets/heima.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!--页面主体区域-->
    <el-container>
      <!--左边侧栏-->
      <el-aside :width="isCollapse ? '64px' : '240px'">
        <el-col>
          <!--伸缩侧边栏按钮-->
          <div class="toggle-button" @click="toggleCollapse">| | |</div>
          <el-menu
            class="el-menu-vertical-demo"
            background-color="#333744"
            text-color="#fff"
            active-text-color="#409eff"
            :unique-opened="uniqueOpened"
            :collapse = "isCollapse"
            :collapse-transition = "collapseTransition"
            :router="true"
            :default-active="activePath"
          >
            <!--一级导航-->
            <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
              <template slot="title">
                <i :class="iconsObj[item.id]"></i>&nbsp;
                <span>{{ item.authName }}</span>
              </template>
                <!--二级导航-->
                <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveNavState('/' + subItem.path)">
                     <i class="el-icon-menu"></i>
                    <span>{{ subItem.authName }}</span>
                </el-menu-item>

            </el-submenu>
          </el-menu>
        </el-col>
      </el-aside>
      <!--主体结构-->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data(){
      return {
        //左侧菜单数据
        menuList : null,
        uniqueOpened : true,
        isCollapse : false,//是否对左侧菜单进行折叠
        collapseTransition : false,
        activePath : '',
        iconsObj : {
         '125' : 'iconfont icon-user',
         '103' : 'iconfont icon-tijikongjian',
         '101' : 'iconfont icon-shangpin',
         '102' : 'iconfont icon-danju',
         '145' : 'iconfont icon-baobiao'
       }
      }
  },
  //当页面加载完毕之后执行
  created(){
    //在created阶段请求左侧菜单数据
    this.getMenuList();
    //当页面加载完毕之后，就在sessionStorga中读取出当前选中的栏目
    this.activePath = window.sessionStorage.getItem('activePath');
  },
  methods: {
    logout() {
      //清除sessionStorage
      window.sessionStorage.clear();
      this.$message({
        message: "退出成功",
        type: "success",
      });
      setTimeout(() => {
        this.$router.push("/login");
      }, 1000);
    },
    //这里发送的是异步请求数据
     async getMenuList(){
      //发送请求获取左侧的数据
      //将得到的数据data给res，data指的是后台返回的数据，这里的data字段是后台内置的
     const {data : res} = await this.$http.get('menus');
     console.log(res)
     if(res.meta.status != 200) return this.$message.error(res.meta.msg);
     //请求到数据之后,将数据给data  里面的menuList
     this.menuList = res.data;
    },
    toggleCollapse(){
      this.isCollapse = !this.isCollapse;
    },
    //点击左边栏目的时候,对当前栏目进行缓存
    saveNavState(activePath){
      window.sessionStorage.setItem('activePath',activePath)
      //缓存之后进行切换
      this.activePath = window.sessionStorage.getItem('activePath')
    }
  }
  
};
</script>

<style scoped>
/*这里需要注意的是，element-ui的标签名称可以直接当做类来使用*/
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #ffffff;
  font-size: 20px;
}
.el-header div {
  display: flex;
  align-items: center;
}
.el-header div span {
  margin-left: 15px;
}
.el-aside {
  background-color: #333744;
}
.el-main {
  background-color: #eaedf1;
}
.toggle-button{
  text-align: center;
  color: #ffffff;
  cursor: pointer;
  padding: 5px 0;
  background-color: rgb(119, 115, 110);
}
</style>
